/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.post.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.poemmanager.domain.Comment;
import me.zhengjie.poemmanager.domain.vo.QueryCriteria;
import me.zhengjie.post.domain.Post;
import me.zhengjie.post.domain.vo.PostQueryCriteria;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
* @author hwl
* @date 2024-12-06
**/
@Mapper
public interface PostMapper extends BaseMapper<Post> {

    IPage<Post> findAll(@Param("criteria") PostQueryCriteria criteria, Page<Object> page);

    List<Post> findAll(@Param("criteria") PostQueryCriteria criteria);
    @Select("select post_id,title,content, created_at,comment_count,favorite_count,like_count from post where user_name=#{userName}")
    List<Post> getPostByUserId(String userName);
    @Select("select post_id, user_name, title, content, created_at, updated_at, status,type, user_name, poem_title, poem_author, poem_id, type, comment_count,like_count,favorite_count  from post where post_id=#{postId}")
    Post getPostById(Integer postId);
    @Select("select post_id as object_id,0 as object_type,content ,title from post where title like CONCAT('%', #{title}, '%')")
    List<QueryCriteria> queryByTitle(String title);
    @Select("select post_id,title,poem_title,poem_author,content,created_at,user_name,comment_count,like_count,favorite_count from post where type=1")
    List<Post> getPoemExplanation();
    @Select("select count(*)  from comment where object_id=#{postId} and object_type=0")
    Integer getCommentCount(Integer postId);
    @Select("select tag_name from tag where object_type=0 and object_id=#{postId}")
    List<String> getTags(Integer postId);
    @Select("select comment_id ,content,user_name ,created_at from comment where object_id=#{postId} and object_type=0")
    List<Comment> getComment(Integer postId);
    @Insert("insert into tag ( tag_name, object_id, object_type) values (#{s},#{postId},0)")
    void insertTag(String s, Integer postId);
    @Select("select post_id,title,poem_title,poem_author,content,created_at,user_name,comment_count,like_count,favorite_count from post where type=0 order by updated_at desc")
    List<Post> getCommunication();
    @Select("select post_id,type,title,poem_title,poem_author,poem_id,content,created_at,updated_at,status,user_name,comment_count,favorite_count,like_count from post where post_id in (select object_id from post_favorites where user_name=#{userName})")
    List<Post> getMyFavoritePost(String userName);


}